import 'dart:async';

import 'package:enterprise_v6_app/app/core/core.dart';

///自定义气体分析倒计时-超时
///[milliseconds] 倒计时毫秒数
///[promptText] 提示文字
class CustomGasCountDownPage extends StatefulWidget {
  const CustomGasCountDownPage({required this.milliseconds, super.key, this.promptText});

  final int milliseconds;
  final String? promptText;

  @override
  State<CustomGasCountDownPage> createState() => _CustomGasCountDownPageState();
}

class _CustomGasCountDownPageState extends State<CustomGasCountDownPage> {
  late Timer _timer;
  int _remainingMilliseconds = 0;

  @override
  void initState() {
    super.initState();
    _remainingMilliseconds = widget.milliseconds;
    //开启计时
    _startTimer();
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  void _startTimer() {
    _timer = Timer.periodic(const Duration(milliseconds: 1000), (timer) {
      setState(() {
        _remainingMilliseconds -= 1000;
      });
    });
  }

  ///转换为两位数
  String twoDigits(int n) {
    if (n >= 10) return '$n';
    return '0$n';
  }

  ///获取计时显示样式
  Widget _getTimingView(String value) {
    return Container(
      height: 20.w,
      padding: EdgeInsets.symmetric(horizontal: 2.w),
      decoration: BoxDecoration(
        color: _remainingMilliseconds > 0 ? const Color(0xFF5C6DFF) : const Color(0xFFFD6B6B),
        borderRadius: BorderRadius.circular(5.w),
      ),
      alignment: Alignment.center,
      child: Text(
        value,
        style: const TextStyle(
          height: 1.2,
          color: Color(0xFFFFFFFF),
          fontSize: 14,
        ),
      ),
    );
  }

  ///分割线
  Widget _getTimingSegmentation() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 8.w),
      child: Text(
        ':',
        style: TextStyle(
          height: 1.2,
          color: _remainingMilliseconds > 0 ? const Color(0xFF5C6DFF) : const Color(0xFFFD6B6B),
          fontSize: 20.sp,
        ),
      ),
    );
  }

  ///倒计时
  Widget _getCountDown() {
    final Duration duration = Duration(milliseconds: _remainingMilliseconds);
    final String twoDigitHours = twoDigits(duration.inHours);
    final String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
    final String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));

    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Stack(
          alignment: Alignment.bottomCenter,
          children: [
            Container(
              height: 5.w,
              width: 114.w,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: [
                    const Color(0xFF5C6DFF).withOpacity(0),
                    const Color(0xFF5C6DFF),
                  ],
                ),
              ),
            ),
            Text(
              '${widget.promptText ?? '气体分析'}倒计时',
              style: TextStyle(
                fontSize: 16.sp,
                height: 1.2,
                color: const Color(0xFF333333),
              ),
            ),
          ],
        ),
        AppGaps.hGap20,
        _getTimingView(twoDigitHours),
        _getTimingSegmentation(),
        _getTimingView(twoDigitMinutes),
        _getTimingSegmentation(),
        _getTimingView(twoDigitSeconds),
      ],
    );
  }

  ///超时
  Widget _getTimeout() {
    final Duration duration = Duration(milliseconds: _remainingMilliseconds);
    final int totalSeconds = duration.inSeconds.abs();
    final String twoDigitHours = twoDigits(totalSeconds ~/ 3600);
    final String twoDigitMinutes = twoDigits((totalSeconds % 3600) ~/ 60);
    final String twoDigitSeconds = twoDigits(totalSeconds % 60);
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Stack(
          alignment: Alignment.bottomCenter,
          children: [
            Container(
              height: 5.w,
              width: 114.w,
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                  colors: [
                    const Color(0xFFFD6B6B).withOpacity(0),
                    const Color(0xFFFD6B6B),
                  ],
                ),
              ),
            ),
            Text(
              '${widget.promptText ?? '气体分析'}已超时',
              style: TextStyle(
                fontSize: 16.sp,
                height: 1.2,
                color: const Color(0xFF333333),
              ),
            ),
          ],
        ),
        AppGaps.hGap20,
        _getTimingView(twoDigitHours),
        _getTimingSegmentation(),
        _getTimingView(twoDigitMinutes),
        _getTimingSegmentation(),
        _getTimingView(twoDigitSeconds),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 15.w),
        alignment: Alignment.center,
        color: const Color(0xFFFFFFFF),
        height: 50.h,
        child: _remainingMilliseconds > 0 ? _getCountDown() : _getTimeout());
  }
}
